package zhieasy.cn.common.strategy;

import org.springframework.stereotype.Component;
import zhieasy.cn.common.vo.ResultVO;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Dahang
 */
@Component
public class FileUpload {
    @Resource
    private InitiationPolicy initiationPolicy;

    @Resource
    private BookMarketPolicy bookMarketPolicy;

    @Resource
    private ChatPolicy chatPolicy;

    @Resource
    private CoursePolicy coursePolicy;

    @Resource
    FoundPolicy foundPolicy;
    //静态类在
    public static class UploadContext {
        //对应的属性
        private final String type;
        //上传策略
        private final UploadStrategy uploadStrategy;
        //上传环境得构造函数
        public UploadContext(String type, UploadStrategy uploadStrategy){
            this.type = type;
            this.uploadStrategy = uploadStrategy;
        }
        //获取对应的上传策略
        public UploadStrategy getUploadStrategy(){
            return uploadStrategy;
        }
        //判断条件
        public boolean options(String type){
            return this.type.equals(type);
        }
    }
    //将每个上传策略加入到list中
    private static final List<UploadContext> argsList = new ArrayList<>();

    @PostConstruct
    public void init(){
        argsList.add(new UploadContext("book_market",bookMarketPolicy));
        argsList.add(new UploadContext("chat",chatPolicy));
        argsList.add(new UploadContext("course",coursePolicy));
        argsList.add(new UploadContext("initiation", initiationPolicy));
        argsList.add(new UploadContext("found",foundPolicy));
    }

    //上传图片，通过对应的上传策略
    public String upload(String type, InputStream inputStream, String objectName){
        UploadStrategy uploadStrategy = null;
        for (UploadContext objectUploadDeal: argsList) {
            if (objectUploadDeal.options(type)){
                //获取对应的上传策略
                uploadStrategy = objectUploadDeal.getUploadStrategy();
                break;
            }
        }
        //通过对应的上传策略上传图片
        assert uploadStrategy != null;
        return uploadStrategy.uploadOneFile(inputStream,objectName);
    }

    /**
     * 传入对应的type，生成对应上传所需要的token+url
     * @param type 对应的类型
     * @param parentId 如果是要传多个对象的话，需要有一个parentId，让我知道到底是哪个的
     * @return R包括token+url
     */
    public ResultVO getTokenAndUrlInBookMarket(String type, String parentId){
        UploadStrategy uploadStrategy = null;
        for (UploadContext objectUploadDeal: argsList) {
            if (objectUploadDeal.options(type)){
                //获取对应的上传策略
                uploadStrategy = objectUploadDeal.getUploadStrategy();
                break;
            }
        }
        //通过对应的url+token
        assert uploadStrategy != null;
        return uploadStrategy.getTokenAndUrl();
    }

    /**
     *
     */
    public ResultVO getTokenAndUrlInBookMarket(String type){
        UploadStrategy uploadStrategy = null;
        for (UploadContext objectUploadDeal: argsList) {
            if (objectUploadDeal.options(type)){
                //获取对应的上传策略
                uploadStrategy = objectUploadDeal.getUploadStrategy();
                break;
            }
        }
        //通过对应的url+token
        assert uploadStrategy != null;
        return uploadStrategy.getTokenAndUrl();
    }
}
